#include "model-helper.h"
#include "order-item-addition-dialog.h"
#include "order-item-addition-model.h"
#include "ui_table-data-addition-dialog.h"

OrderItemAdditionDialog::OrderItemAdditionDialog(QWidget *parent) :
    TableDataAdditionDialog(parent)
{
}

void OrderItemAdditionDialog::initialize()
{
    m_abstractModel = new OrderItemAdditionModel(this);
    ui->label->setText("Produkty poza zamówieniem:");
    resize(550, 300);
    TableDataAdditionDialog::initialize();
}

void OrderItemAdditionDialog::accept()
{
    QList<int> selectedRows = ModelHelper::normalSelectedRows(ui->tableView);
    QVariantList dataList;
    QVariant orderId = m_sectionInfo.loadingParameters().at(0);
    for (int i = 0; i < selectedRows.size(); i++)
    {
        int row = selectedRows.at(i);
        dataList << m_abstractModel->id(row, 1) << orderId << m_abstractModel->id(row, 0)
                 << m_abstractModel->index(row, OrderItemAdditionModel::VatColumn).data(Qt::UserRole)
                 << m_abstractModel->index(row, OrderItemAdditionModel::QuantityColumn).data(Qt::UserRole)
                 << m_abstractModel->index(row, OrderItemAdditionModel::NameColumn).data(Qt::UserRole)
                 << m_abstractModel->index(row, OrderItemAdditionModel::PriceColumn).data(Qt::UserRole);
    }
    emit addDataRequested(dataList);
    AbstractDataAdditionDialog::accept();
}
